<template>
  <Container>
    <template v-slot:searchForm>
      <div class="searchRow">
        <el-form class="elForm" ref="form" :model="searchFormData" label-width="80px">
          <el-form-item label="维保标题">
            <el-input v-model="searchFormData.title" clearable placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="设备编号">
            <el-input v-model="searchFormData.deviceCode" clearable placeholder="请输入"></el-input>
          </el-form-item>
        </el-form>
        <div class="btnList">
          <el-button type="danger" icon="el-icon-delete" @click="handleClickDel(selectionList)"
            :disabled="selectionList.length == 0" plain>删除</el-button>
          <el-button type="primary" icon="el-icon-circle-plus-outline" @click="handleClickAddEdit('add')"
            plain>添加</el-button>
          <el-button type="primary" icon="el-icon-search" @click="queryTableList" plain>查 询</el-button>
        </div>
      </div>
    </template>
    <template v-slot:table>
      <el-table :data="tableData" border height="100%" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="title" label="设备维保标题" width="150">
        </el-table-column>
        <el-table-column prop="deviceCode" label="维保设备编号" width="150">
        </el-table-column>
        <el-table-column prop="maintainDate" label="维保日期" width="150">
        </el-table-column>
        <el-table-column prop="remark" label="维保描述">
        </el-table-column>
        <el-table-column prop="maintainStatus" label="维保状态">
          <template slot-scope="scope">
            <span v-show="scope.row.maintainStatus == 'UNTREATED'">未处理</span>
            <span v-show="scope.row.maintainStatus == 'INPROGRESS'">进行中</span>
            <span v-show="scope.row.maintainStatus == 'END'">结束</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button type="text" v-if="scope.row.maintainStatus == 'UNTREATED'"
              @click="handleClickAddEdit(scope.row, 'handle')">维保开始</el-button>
            <el-button type="text" v-if="scope.row.maintainStatus == 'INPROGRESS'"
              @click="handleClickAddEdit(scope.row, 'handle')">维保完成</el-button>
            <el-button type="text"  v-if="scope.row.maintainStatus == 'END'"
              @click="handleClickAddEdit(scope.row, 'handle')">查看详情</el-button>
            <!-- <el-button type="text"  @click="handleClickAddEdit(scope.row, 'edit')">编辑</el-button> -->
            <el-button type="text" @click="handleClickDel([scope.row])">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <AddEdit :title="'维保信息'" @closeDialog="closeDialog" :addEditFlag="addEditFlag" :dialogVisible="dialogVisible"
        :data="formData" :deviceList="deviceList">
      </AddEdit>
    </template>
    <template v-slot:pagination>
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="page" :page-sizes="[10, 100, 200, 300, 400, 500, 600]" :page-size="size"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </template>
  </Container>
</template>

<script>
import Container from "@/components/Container"
import AddEdit from './components/addEdit';
import maintain from "@/api/maintain";
import device from "@/api/device";
export default {
  components: { Container, AddEdit },
  data() {
    return {
      tableData: [],
      selectionList: [],
      searchFormData: {},
      deviceList: [],
      page: 1,
      size: 100,
      total: 10,
      addEditFlag: 'add',
      dialogVisible: false,
      formData: {}
    }
  },
  watch: {},
  mounted() {
    this.queryTableList();
    this.queryDeviceList();
  },
  methods: {
    async queryDeviceList() {
      let { data } = await device.queryList();
      if (!data || data.length == 0) {
        // this.$message.warning('请先创建设备')
      } else {
        this.deviceList = data || [];
      }
    },
    queryTableList() {
      let params = {
        page: this.page,
        size: this.size,
        condition: {
          ...this.searchFormData
        }
      }
      maintain.queryPageList(params).then(res => {
        let { data } = res
        this.total = data.total;
        this.tableData = data.list;
      }).catch(err => {
        console.log(err)
      })
    },
    closeDialog() {
      this.dialogVisible = false;
      this.formData = {};
      this.queryTableList();
    },
    handleClickDel(rowList = []) {
      let list = [];
      rowList.forEach(item => {
        list.push(item.maintainId)
      })
      if (rowList.length == 0) return;
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        maintain.delList(list).then(res => {
          if (res.code = 1) {
            this.queryTableList();
            this.$message.success('删除成功')
          } else {
            this.$message.warning('删除失败')
          }
        }).catch(err => {
          this.$message.warning('删除失败')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    handleClickAddEdit(row = {}, flag = 'add') {
      this.formData = JSON.parse(JSON.stringify(row));
      this.addEditFlag = flag
      this.dialogVisible = true;
    },
    handleSelectionChange(list = []) {
      this.selectionList = list
    },
    handleSizeChange(size) {
      this.size = size;
      this.queryTableList();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.queryTableList();
    },
  }
}
</script>

<style lang="scss" scoped>
.row {
  padding-bottom: 15px;
}
</style>
